package com.sims.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sims.dao.ISimsClassDao;
import com.sims.eo.SimsClass;
import com.sims.util.HibernateSessionFactory;

public class SimsClassDaoImp implements ISimsClassDao {

	@Override
	public SimsClass getSimsClassbyClassName(String className) {
		// 从数据库获取对象
		SimsClass simsClass = null;
		String sql = "from SimsClass s where s.className=?";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setParameter(0, className);
		List classs = query.list();
		if (classs.size() != 0) {
			simsClass = (SimsClass) classs.get(0);
		}
		HibernateSessionFactory.closeSession();
		return simsClass;
	}

	@Override
	public List<SimsClass> getAllSimsClass() {
		String sql = "from SimsClass";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		List<SimsClass> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public boolean deletSimsClassById(Long id) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 查询删除对象
			SimsClass class1 = (SimsClass) session.get(SimsClass.class, id);
			// 持久化操作
			session.delete(class1);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public SimsClass getSimsClassById(Long id) {
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 查询删除对象
		SimsClass class1 = (SimsClass) session.get(SimsClass.class, id);
		return class1;
	}

	@Override
	public boolean updateSimsClass(SimsClass simsClass) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.update(simsClass);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public List<SimsClass> getAllSimsClass(int pageNo, int pageSize) {
		String sql = "from SimsClass";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<SimsClass> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public long getRowCount() {
		String hql = "select count(id) from SimsClass";
		long count = 0;
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long getRowCountByClassName(String className) {
		String hql = "select count(className) from SimsClass s where s.className=?";
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		query.setParameter(0, className);
		long count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long createClass(SimsClass simsClass) {
		Long id = 0L;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.save(simsClass);
			// 提交事务
			trans.commit();
			id = simsClass.getId();
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return id;
	}

}
